package com.ningxun.pagentcrawler.util;

import com.ningxun.pagentcrawler.entity.policy.PolicyItem;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 文件工具类
 * 负责文件操作相关功能
 */
public class FileUtil {

    /**
     * 保存爬虫结果到文件
     */
    public static void saveCrawlerResult(List<PolicyItem> policies) {
        try (PrintWriter writer = new PrintWriter(new FileWriter("hebei_policies.txt", true))) {
            writer.println("=== 河北省政府政策爬取结果 ===");
            writer.println("爬取时间: " + new Date());
            writer.println("目标网站: https://www.hebei.gov.cn/columns/49f13cc2-db03-4d0c-b4fe-2f3f659d3b6e/index.html");
            writer.println("说明: 此文件包含爬取到的政策标题、链接和内容，已自动导入到政策管理系统");
            writer.println();
            
            for (int i = 0; i < policies.size(); i++) {
                PolicyItem policy = policies.get(i);
                writer.println((i + 1) + ". " + policy.toString());
                if (policy.getContent() != null && !policy.getContent().isEmpty()) {
                    writer.println("   内容: " + policy.getContent().substring(0, Math.min(200, policy.getContent().length())) + "...");
                }
                writer.println();
            }
            
            writer.println();
            System.out.println("爬虫结果已保存到 hebei_policies.txt 文件");
            System.out.println("文件位置: " + System.getProperty("user.dir") + "/hebei_policies.txt");
            
        } catch (IOException e) {
            System.err.println("保存文件失败: " + e.getMessage());
        }
    }
} 